// 给eletron(node)端注入外部对象或者服务接口
// Renderer进程通过 window.xxx.yyy 方式调用

const { contextBridge, remote } = require("electron");
const { ipcRenderer: ipc } = window.require('electron-better-ipc'); // 更简单的ipc封装工具，详见 https://github.com/sindresorhus/electron-better-ipc
const CONSTS = require('./consts');

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('remote', remote);
contextBridge.exposeInMainWorld('require', require);
contextBridge.exposeInMainWorld('api', {
    getUser: async (userName) => {
        return await ipc.callMain(CONSTS.GET_USER_BY_NAME, userName);
    },
    closeModal: () => {
        ipc.callMain(CONSTS.CLOSE_LAST_MODAL);
    },
    getSystemInfo: async () => {
        return await ipc.callMain(CONSTS.GET_SYSTEM_INFO);
    },
    getDbConnectionInfo: async () => {
        return await ipc.callMain(CONSTS.GET_DB_CONFIG);
    },
    alert: (msg) => {
        return ipc.callMain(CONSTS.SHOW_ALERT, msg);
    }
});